package com.ykdl.member.kid.image;

import java.io.InputStream;
import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;

public class ImageFactory {

	private static boolean TEST = false;
	public static final String TAG = "ImageFactory";
	public static Vector<Object[]> imagePool = new Vector<Object[]>();
	private static Object lock = new Object();

	public static Bitmap createBitmap(Bitmap source, int x, int y, int width,
			int height, Matrix m, boolean filter, String tag) {
		Bitmap bitmap = Bitmap.createBitmap(source, x, y, width, height, m,
				filter);
		if (TEST) {
			synchronized (lock) {
				imagePool.add(new Object[] { tag, bitmap });
				Log.e(
						TAG,
						"createBitmap tag[" + tag + "] width["
								+ bitmap.getWidth() + "] height["
								+ bitmap.getHeight() + "]  hashcode["
								+ bitmap.hashCode() + "]");
			}
		}
		return bitmap;
	}

	public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
			int dstHeight, boolean filter, String tag) {
		Bitmap bitmap = Bitmap.createScaledBitmap(src, dstWidth, dstHeight,
				filter);
		if (TEST) {
			synchronized (lock) {
				imagePool.add(new Object[] { tag, bitmap });
				Log.e(TAG, "createScaledBitmap tag[" + tag + "] width["
						+ bitmap.getWidth() + "] height[" + bitmap.getHeight()
						+ "]  hashcode[" + bitmap.hashCode() + "]");
			}
		}
		return bitmap;
	}

	public static Bitmap createBitmap(int width, int height,
			android.graphics.Bitmap.Config config, String tag) {
		Bitmap bitmap = Bitmap.createBitmap(width, height, config);
		if (TEST) {
			synchronized (lock) {
				imagePool.add(new Object[] { tag, bitmap });
				Log.e(TAG, "createBitmapconfig tag[" + tag + "] width["
						+ bitmap.getWidth() + "] height[" + bitmap.getHeight()
						+ "]  hashcode[" + bitmap.hashCode() + "]");
			}
		}
		return bitmap;
	}

	public static Bitmap decodeByteArray(byte[] data, int offset, int length,
			String tag) {
		Bitmap bitmap = BitmapFactory.decodeByteArray(data, offset, length);
		if (TEST) {
			synchronized (lock) {
				imagePool.add(new Object[] { tag, bitmap });
				Log.e(TAG, "decodeByteArray tag[" + tag + "] width["
						+ bitmap.getWidth() + "] height[" + bitmap.getHeight()
						+ "]  hashcode[" + bitmap.hashCode() + "]");
			}
		}
		return bitmap;
	}

	public static Bitmap decodeStream(InputStream is, String tag) {
		Bitmap bitmap = BitmapFactory.decodeStream(is);
		if (TEST) {
			synchronized (lock) {
				imagePool.add(new Object[] { tag, bitmap });
				Log.e(
						TAG,
						"decodeStream tag[" + tag + "] width["
								+ bitmap.getWidth() + "] height["
								+ bitmap.getHeight() + "]  hashcode["
								+ bitmap.hashCode() + "]");
			}
		}
		return bitmap;
	}
	public static Bitmap decodeFile(String path,String tag){
		Bitmap bitmap = BitmapFactory.decodeFile(path);
		if (TEST) {
			synchronized (lock) {
				imagePool.add(new Object[] { tag, bitmap });
				Log.e(
						TAG,
						"decodeStream tag[" + tag + "] width["
								+ bitmap.getWidth() + "] height["
								+ bitmap.getHeight() + "]  hashcode["
								+ bitmap.hashCode() + "]");
			}
		}
		return bitmap;
	}

	public static void recycle(Bitmap bitmap) {
		if (TEST) {
			synchronized (lock) {
				int index = contains(bitmap);
				if (index == -1) {
					Log.e(
							TAG,
							"recycle didnot contains bitmap  width["
									+ bitmap.getWidth() + "] height["
									+ bitmap.getHeight() + "]  hashcode["
									+ bitmap.hashCode() + "]");
				} else {
					Object[] data = imagePool.remove(index);
					Log.e(
							TAG,
							"recycle  bitmap tag[" + data[0] + "] width["
									+ bitmap.getWidth() + "] height["
									+ bitmap.getHeight() + "]  hashcode["
									+ bitmap.hashCode() + "]");
				}
			}
		}
		bitmap.recycle();
	}

	private static int contains(Bitmap bitmap) {
		for (int i = 0; i < imagePool.size(); i++) {
			Object[] data = imagePool.get(i);
			if (data[1] == bitmap) {
				return i;
			}
		}
		return -1;
	}

	public static void log() {
		if (TEST) {
			synchronized (lock) {
				Log.e(TAG, "<<<<<<<<<<<<=============");
				for (int i = 0; i < imagePool.size(); i++) {
					Object[] data = imagePool.get(i);
					Log.e(TAG, "tag[" + data[0] + "] width["
							+ ((Bitmap) data[1]).getWidth() + "] height["
							+ ((Bitmap) data[1]).getHeight() + "]  hashcode["
							+ ((Bitmap) data[1]).hashCode() + "]");
				}
				Log.e(TAG, "============>>>>>>>>>>>>");
			}
		}
	}

}
